﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using DBUtlity;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class UpLoadService
    {
        /// <summary>
        /// 新增（上传）文件
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="fileAddress"></param>
        /// <returns></returns>
        public int InsertUpLoadFile(int workParentId, string fileAddress, string fileName)
        {
            string sql = "Insert into WorkflowUpLoad(WorkParentId,FileAddress,FileName) ";
            sql += "values (@WorkParentId,@FileAddress,@FileName)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@FileAddress",fileAddress),
                 new SqlParameter("@FileName",fileName)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 查询是否有相同文件名的文件
        /// </summary>
        /// <param name="fileAddress"></param>
        /// <returns></returns>
        public int GetUpLoadFile(string fileAddress)
        {
            string sql = "select count(*) from WorkflowUpLoad where ";
            sql += "FileAddress=@FileAddress";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@FileAddress",fileAddress)
            };
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, param));
        }
        /// <summary>
        /// 查询上传文件路径
        /// 需求参数 ID
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public List<WorkflowUpLoad> GetUpLoadFile(int workParentId,int no)
        {
            string sql = "select Id,WorkParentId,UpLoadTime,FileAddress,FileName ";
            sql += "from WorkflowUpLoad where WorkParentId=@WorkParentId and Del=0";
            if (no == -1) { sql += " and FileAddress like '%-IE-%'"; }
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<WorkflowUpLoad> list = new List<WorkflowUpLoad>();
            while (objReader.Read())
            {
                list.Add(new WorkflowUpLoad()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    UpLoadTime = Convert.ToDateTime(objReader["UpLoadTime"]),
                    FileAddress = objReader["FileAddress"].ToString(),
                    FileName = objReader["FileName"].ToString()
                });
            }
            objReader.Close();
            return list;
        }
    }
}
